﻿namespace mPassRT.Common
{
    using System;

    using Microsoft.Practices.Unity;

    using Windows.UI.Xaml;

    /// <summary>Extension methods for the App class.</summary>
    public static class AppExtensions
    {
        /// <summary>The container resource key.</summary>
        public const string ContainerResourceKey = "__UnityContainer";

        /// <summary>Gets the container from the specified app.</summary>
        /// <param name="app">The app.</param>
        /// <returns>The unity container.</returns>
        public static UnityContainer GetContainer(this Application app)
        {
            if (app.Resources.ContainsKey(ContainerResourceKey))
            {
                return (UnityContainer)app.Resources[ContainerResourceKey];
            }
            else
            {
                var container = new UnityContainer();
                app.Resources.Add(ContainerResourceKey, container);
                return container;
            }
        }

        /// <summary>Gets the container from the specified app.</summary>
        /// <param name="app">The app.</param>
        /// <param name="newContainer">will be set to true if the container was created as part of the call</param>
        /// <returns>The unity container.</returns>
        public static UnityContainer GetContainer(this Application app, out bool newContainer)
        {
            newContainer = app.Resources.ContainsKey(ContainerResourceKey) == false;
            return GetContainer(app);
        }
    }
}
